//
//  XHLoginViewModel.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-30.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHLoginViewModel.h"

@interface XHLoginViewModel()

@property (nonatomic, strong) RACCommand *loginCommand;

@end
@implementation XHLoginViewModel

- (id)init
{
    self = [super init];
    if (self) {
        
        self.hasLogin = NO;
        self.showError = NO;
        self.showNetError = NO;
       
        

    }
    return self;
}

- (void)doLogin
{
    @weakify(self);
    
    [[XHNetWorkManager loginWithName:self.username PassWord:self.password] subscribeNext:^(id response) {
        @strongify(self);
        if ([response isKindOfClass:[NSDictionary class]]) {
            NSDictionary *data = (NSDictionary *)response;
            id message = [data objectForKey:@"message"];
            NSDictionary *dic ;
            if ([message isKindOfClass:[NSDictionary class]]) {
                dic = (NSDictionary *)message;
            }
            if ([message isKindOfClass:[NSString class]]) {
                NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding];
                
               dic = [NSJSONSerialization JSONObjectWithData:messageData options:0 error:nil];
            }
            //NSLog(@"dic = %@",data);
            id UID = [[data objectForKey:@"vo"] objectForKey:@"id"];
            NSLog(@"id= %@",UID);
            if (UID) {
                NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
                [defaults setObject:UID forKey:@"UID"];
                [defaults synchronize];
            }
            if ( [[dic objectForKey:@"result"] isEqualToString:@"success"]) {
                self.hasLogin = YES;
            }else{
                self.showError = YES;
            }
        }else{
           
             self.showNetError = YES;
        }
        
    }];
}


@end
